﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Collections.Generic;
using NetBay.Core.BusinessEntities;
using NetBay.Core.Log;
using Rio.Framework.Historical;
using System.Data;
using NetBay.Core.BusinessEntities.Versioning;
using NetBay.Core.Extensions;
using NetBay.Core.BusinessEntities.Workflow;
#endregion

namespace Rio.Doc.Business.Repository.Translator
{
    /// <summary>
    /// Class used to convert DocumentFolder database row to DocumentFolder object
    /// </summary>
    public class DocumentTranslator
    {
        /// <summary>
        /// Translate a bunch of DocumentType DataReader to DocumentType object
        /// </summary>
        /// <param name="p_oIDataReader">The dataReader that contains the rows</param>
        /// <returns>A list of DocumentFolder Object</returns>
        public static List<Document> TranslateDocuments(IDataReader p_oIDataReader)
        {
            var l_lstDocuments = new List<Document>();
            
            try
            {
                using (p_oIDataReader)
                {
                    // Read all rows
                    while (p_oIDataReader.Read())
                    {
                        TranslateOne(p_oIDataReader, l_lstDocuments);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_lstDocuments;
        }

        private static void TranslateOne(IDataReader p_oIDataReader, List<Document> p_oDocuments)
        {
            var l_oDocument = new Document();
            
            try
            {
                l_oDocument.Identifier = Convert.ToInt32(p_oIDataReader["rd_no"].ToString());
                l_oDocument.FolderId = Convert.ToInt32(p_oIDataReader["folder_Id"].ToString());
                l_oDocument.FolderIdentifier = p_oIDataReader["rd_iddoc"].ToString();
                l_oDocument.ImageName = p_oIDataReader["rd_idimage"].ToString();
                l_oDocument.ItemRank = p_oIDataReader["rd_item"].ToString();
                l_oDocument.RelativePath = p_oIDataReader["rd_path"].ToString();
                l_oDocument.FolderTypeIdentifier = Convert.ToInt64(p_oIDataReader["rd_td"].ToString());
                l_oDocument.RankInFolder = Convert.ToInt32(p_oIDataReader["rd_rang"].ToString());
                l_oDocument.Note = p_oIDataReader["rd_note"].ToString();
                l_oDocument.Deleted = (bool) FieldRow.Translate(p_oIDataReader["rd_suppr"].ToString());
                l_oDocument.FullText = p_oIDataReader["rd_fulltext"].ToString();
                l_oDocument.Author = p_oIDataReader["rd_auteur"].ToString();
                l_oDocument.PhotoCredit = p_oIDataReader["rd_credit_photo"].ToString();
                l_oDocument.VideoLength = p_oIDataReader["rd_duree_video"].ToString();
                l_oDocument.PublicationDate = FieldRow.Translate<DateTime>(p_oIDataReader["rd_publie_le"].ToString());
                l_oDocument.ModificationDate = FieldRow.Translate<DateTime>(p_oIDataReader["rd_datemod"].ToString());
                l_oDocument.FolderHumanIdentifier = Convert.ToInt32(p_oIDataReader["rd_id_dossier"].ToString());
                l_oDocument.ImageHumanIdentifier = Convert.ToInt32(p_oIDataReader["rd_id"].ToString());
                l_oDocument.OriginalNameFile = p_oIDataReader["rd_fichier_origine"].ToString();
                l_oDocument.MultiVolumePath = p_oIDataReader["rd_pathmultivol"].ToString();

                //WkfState 
                try
                {
                    l_oDocument.WorkflowState = p_oIDataReader.GetEnumValue<WorkflowState>("t_ref_wkf_state_id");
                }
                catch
                {
                    l_oDocument.WorkflowState = WorkflowState.None;
                }
              
                // Spécific process for 'Thumbnail' member
                var l_bThumbnail = false;
                switch (Convert.ToInt32(p_oIDataReader["rd_imagette"].ToString()))
                {
                    case 2:
                        l_bThumbnail = true;
                        break;
                    default:
                        break;
                }
                l_oDocument.Thumbnail = l_bThumbnail;

                // All members are readed, so save this document to collection
                p_oDocuments.Add(l_oDocument);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }
        }

        #region Versioning

        /// <summary>
        /// Complete document list with all version.
        /// </summary>
        /// <returns></returns>
        public static List<VersioningItem> TranslateDocumentsWithAllVersion(IDataReader p_oIDataReader)
        {
            var l_oAllVersionItem = new List<VersioningItem>();

            try
            {
                using (p_oIDataReader)
                {
                    while (p_oIDataReader.Read())
                    {
                        var l_oVersionInfo = new VersioningItem();

                        l_oVersionInfo.VersionId = Convert.ToInt32(p_oIDataReader["RDV_ID"].ToString());
                        l_oVersionInfo.Major = Convert.ToInt32(p_oIDataReader["RDV_MAJOR"].ToString());
                        l_oVersionInfo.Minor = Convert.ToInt32(p_oIDataReader["RDV_MINOR"].ToString());
                        l_oVersionInfo.Note = p_oIDataReader["RDV_NOTE"].ToString();
                        l_oVersionInfo.Author = p_oIDataReader["RDV_AUTEUR"].ToString();
                        l_oVersionInfo.PhotoCredit = p_oIDataReader["RDV_CREDIT_PHOTO"].ToString();
                        l_oVersionInfo.State = (VersionState)System.Enum.Parse(typeof(VersionState), p_oIDataReader["T_REF_STATE_ID"].ToString());
                        l_oVersionInfo.IsCurrentVersion = Convert.ToBoolean(int.Parse(p_oIDataReader["IS_CURRENT_VERSION"].ToString()));
                        l_oVersionInfo.IsOriginalVersion = Convert.ToBoolean(int.Parse(p_oIDataReader["IS_ORIGINAL_VERSION"].ToString()));
                        l_oVersionInfo.DocumentPath = p_oIDataReader["DOCUMENT_PATH"].ToString();
                        l_oVersionInfo.CreatedBy = p_oIDataReader["USERCREE"].ToString();
                        l_oVersionInfo.CreatedOn = DateTime.Parse(p_oIDataReader["RDV_DATECREE"].ToString());
                        l_oAllVersionItem.Add(l_oVersionInfo);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_oAllVersionItem;
        }

        /// <summary>
        /// Translate the result of the major minor creation oeration. Return a new version id or -1 if the version is allready in use.
        /// </summary>
        /// <returns></returns>
        public static long TranslateAddVersion(IDataReader p_oIDataReader)
        {
            long l_oRet = 0;
            try
            {
                using (p_oIDataReader)
                {
                    if (p_oIDataReader.Read())
                    {
                        l_oRet = Convert.ToInt64(p_oIDataReader["RDV_ID"].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }
            return l_oRet;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_lRdvId"></param>
        /// <returns></returns>
        public static VersioningItem GetVersion(IDataReader p_oIDataReader)
        {
            var l_oAllVersionItem = new VersioningItem();

            try
            {
                using (p_oIDataReader)
                {
                    if (p_oIDataReader.Read())
                    {
                        l_oAllVersionItem.VersionId = Convert.ToInt32(p_oIDataReader["RDV_ID"].ToString());
                        l_oAllVersionItem.Major = Convert.ToInt32(p_oIDataReader["RDV_MAJOR"].ToString());
                        l_oAllVersionItem.Minor = Convert.ToInt32(p_oIDataReader["RDV_MINOR"].ToString());
                        l_oAllVersionItem.Note = p_oIDataReader["RDV_NOTE"].ToString();
                        l_oAllVersionItem.Author = p_oIDataReader["RDV_AUTEUR"].ToString();
                        l_oAllVersionItem.OwnerId = Convert.ToInt64(p_oIDataReader["RDV_USERCREE"]);
                        l_oAllVersionItem.PhotoCredit = p_oIDataReader["RDV_CREDIT_PHOTO"].ToString();
                        l_oAllVersionItem.State = (VersionState)System.Enum.Parse(typeof(VersionState), p_oIDataReader["T_REF_STATE_ID"].ToString());
                        l_oAllVersionItem.IsCurrentVersion = Convert.ToBoolean(int.Parse(p_oIDataReader["IS_CURRENT_VERSION"].ToString()));
                        l_oAllVersionItem.IsOriginalVersion = Convert.ToBoolean(int.Parse(p_oIDataReader["IS_ORIGINAL_VERSION"].ToString()));
                        l_oAllVersionItem.DocumentPath = p_oIDataReader["DOCUMENT_PATH"].ToString();
                        l_oAllVersionItem.CreatedBy = p_oIDataReader["USERCREE"].ToString();
                        l_oAllVersionItem.CreatedOn = DateTime.Parse(p_oIDataReader["RDV_DATECREE"].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_oAllVersionItem;
        }

        /// <summary>
        /// Translate DA - HasVersionInCheckout function result
        /// </summary>
        /// <param name="p_oIDataReader"></param>
        /// <returns></returns>
        public static long GetCurrentVersion(IDataReader p_oIDataReader)
        {
            long result = 0;
            try
            {
                using (p_oIDataReader)
                {
                    if (p_oIDataReader.Read())
                    {
                        result = Convert.ToInt64(p_oIDataReader["RESULT"].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }
            return result;
        }

        #endregion

    }
}

